package com.platform.car.app;

import com.platform.car.model.Capture;
import com.platform.car.model.Cars;
import com.platform.car.model.Parkuse;
import com.platform.car.service.*;
import com.platform.upms.model.json.Tip;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 车辆管理 客户端 Controller
 *
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/car/app/cars")
public class AppCarsController {

    /**
     * 2017-11-14 13:10:38 韩榕榕 迁移
     * @param cars (携带 userId:用户的ID,numberPlates:车牌号，status:状态(1：默认车辆，2：普通车辆))userId 用户ID  车牌号码   是否默认车辆  1：默认  2 普通
     * @return 成功状态
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Tip save(Cars cars) {
        try {
            if(carsService.countNumberPlates(cars)>0){
                return  new Tip(2,"您已绑定此车牌！请重新输入车牌号！");
            }
            if(carsService.countNumber(cars) > 4){//一个用户最多绑定五个
                return  new Tip(2,"超过数量限制");
            }
            saveCars(cars);//绑定车牌;
            return judgeCars(cars);

        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "添加失败！");
        }
    }


    /**
     * 绑定车牌
     * 2017-11-14 14:40:26 韩榕榕 创建
     */
    private void saveCars(Cars cars){
        List<Cars> carsList = carsService.selectByUserId(cars);
            if (cars.getStatus().equals("1")) {
                if (carsList.size() > 0) {
                    for (Cars c : carsList) {
                        c.setStatus("2");
                        carsService.update(c);
                    }
                }
            }
            cars.setCreateTime(new Date());
            carsService.saveData(cars);
    }

    /**
     * 判断该用户是否进场之后下载app并绑定车牌
     * 2017-11-14 14:40:36 韩榕榕 创建
     * @param cars
     * @return
     */
    private Tip judgeCars(Cars cars){
        Capture capture=new Capture(cars.getNumberPlates(),"0");//用户拍照后进场没有预约
        List<Capture> captures = captureService.selectByPlate(capture);
        if(captures.size()>0){//进入停车场拍照后下载并注册的APP向预约表中插入一条数据，方便线上结算
            Capture c=captures.get(0);
            Parkuse p=new Parkuse(0,cars.getNumberPlates());//未预约的用户的ID用0标识
            Parkuse parkuse = parkuseService.selectOrder(p);
            parkuse.setIntro("进场后注册的APP");
            parkuse.setUserId(cars.getUserId());
            parkuseService.update(parkuse);//预约表中添加一条数据
            c.setType("1");//修改拍照记录中的类型为已预约
            captureService.update(c);
        }
        return  new Tip("添加成功");
    }


    /**
     * 查询某用户下的所有车辆
     * 2017-11-14 14:42:36 韩榕榕 迁移
     * @param cars (携带userId)
     * @return
     */
    @RequestMapping(value = "/selectForListByUserId")
    @ResponseBody
    public Tip selectForListByUserId(Cars cars) {
        List<Cars> carsList = carsService.selectByUserId(cars);
        return new Tip(carsList);
    }

    /**
     * 修改绑定车牌的信息
     * @param cars (携带：userId:用户的Id,status:标识（1：默认，2：普通），numberPlates:车牌号，id:)
     * @return
     */
    @RequestMapping(value = "/updateCar")
    @ResponseBody
    public Tip updateCar(Cars cars) {
        try {
            judgeStatus(cars);
            if (carsService.get(cars.getId()).getNumberPlates().equals(cars.getNumberPlates())) {       //根据车牌id判断原车牌号码和现车牌号码是否一致，相同直接修改其状态，不同修改车牌号码和其状态
                carsService.update(cars);
            } else {
                if (carsService.countNumberPlates(cars) > 0) {   //判断此用户是否添加了同一个车牌
                    return new Tip(2, "您已绑定此车牌！请重新输入车牌号！");
                }
                carsService.update(cars);
            }
            return new Tip("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "数据修改异常");
        }
    }

    /**
     * 判断绑定车辆的状态
     * @param cars
     */
    private  void judgeStatus(Cars cars){
        List<Cars> carsList = carsService.selectByUserId(cars);//查询用户绑定的车辆
        if (cars.getStatus().equals("1")) {
            if (carsList.size() > 0) {
                for (Cars c : carsList) {
                    c.setStatus("2");
                    carsService.update(c);
                }
            }
        }
    }

    /**
     * 删除绑定的车辆
     * @param cars   车辆（车辆的ID）
     * @return
     */
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Tip delete(Cars cars) {
        try {
            carsService.deleteDataById(cars);
            return new Tip("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "删除失败！");
        }

    }

    /**
     * 根据车牌号删除
     *@param cars (携带 userId:用户的ID,numberPlates:车牌号)
     * @return
     */
    @RequestMapping(value = "/deleteByPlates")
    @ResponseBody
    public Tip deleteByPlates(Cars cars) {
        try {
            Cars c = carsService.selectByPlates(cars);
            if (cars != null) {
                    carsService.deleteDataById(c);
                    return new Tip("删除成功");
            } else {
                return new Tip(2, "车牌号有误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "数据修改异常");
        }
    }

    /**
     * 根据user_id删除
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/deleteByUserId")
    @ResponseBody
    public Tip deleteByUserId(Integer userId) {
        try {
            carsService.deleteByUserId(userId);
            return new Tip("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "数据删除异常");
        }

    }


    /**
     * 修改车的标志
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateCarStatus")
    @ResponseBody
    public Tip updateCarStatus(String id, String status) {
        try {
            Cars cars = carsService.get(id);
            List<Cars> carsList = carsService.selectByUserId(cars);
            for (Cars c : carsList) {
                if (c.getStatus().equals("1")) {
                    c.setStatus("2");
                    carsService.update(c);
                }
            }
            cars.setStatus(status);
            carsService.update(cars);
            return new Tip("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "数据修改失败");
        }
    }


    //---------------------------- property -------------------------------

    @Resource
    private CarsService carsService;

    @Resource
    private CaptureService captureService;

    @Resource
    private ParkuseService parkuseService;

    @Resource
    private CameraService cameraService;
    @Resource
    private ParkinglotService parkinglotService;

}
